package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.*;
import cc.rengu.igas.bomp.common.dao.impl.*;
import cc.rengu.igas.bomp.common.entity.*;
import cc.rengu.igas.bomp.common.enums.MchntFeeTypeEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.ExportMchntInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntBatchExportRequest;
import cc.rengu.igas.bomp.facade.response.MchntBatchExportResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhangxuran on 2020/03/30
 */
public class MchntBatchExportService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntBatchExportRequest mchntBatchExportRequest = new MchntBatchExportRequest();
        ConvertUtil.convertOutput(mchntBatchExportRequest);
        return mchntBatchExportRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        MchntBatchExportRequest mchntBatchExportRequest = (MchntBatchExportRequest) request;
        /*获取法人行号信息*/
        String instId = mchntBatchExportRequest.getHeader().getInstId();
        MchntBatchExportResponse mchntBatchExportResponse = new MchntBatchExportResponse();
        /* 获取请求报文信息，用户标识、商户号、商户名称、签约机构、代理商、商户等级、商圈、商户状态、当前页数、页显示记录数*/
        /* 获取用户标识信息 */
        String userId = mchntBatchExportRequest.getUserId();
        /* 获取商户号信息 */
        String mchntNo = mchntBatchExportRequest.getMchntNo();
        /* 获取商户名称信息 */
        String mchntName = mchntBatchExportRequest.getMchntName();
        /* 获取签约机构信息 */
        String signInstId = mchntBatchExportRequest.getSignInstId();
        /* 获取代理商户号信息 */
        String agentMchntNo = mchntBatchExportRequest.getAgentMchntNo();
        /* 获取商户等级信息 */
        String mchntLevel = mchntBatchExportRequest.getMchntLevel();
        /* 获取商圈信息 */
        String mchntArea = mchntBatchExportRequest.getMchntArea();
        /* 获取商户状态信息 */
        String mchntStatus = mchntBatchExportRequest.getMchntStatus();
        /* 获取平台商户信息 */
        String platMchntNo = mchntBatchExportRequest.getPlatMchntNo();
        /* 获取当前页数信息 */
        int pageIndex = mchntBatchExportRequest.getPageIndex();
        /* 获取当前页显示记录数 */
        int pageSize = mchntBatchExportRequest.getPageSize();
        /*必填性校验*/
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(userId)) {
            rglog.error("必填性校验失败!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);

        MchntBaseInfo mchntBaseInfoFilter = new MchntBaseInfo();
        /*法人行赋值*/
        mchntBaseInfoFilter.setInstId(instId);
        /*商户号过滤条件*/
        if (!StringUtil.isEmptyOrNull(mchntNo)) {
            mchntBaseInfoFilter.setMchntNo("%" + mchntNo + "%");
        }
        /*商户名过滤条件*/
        if (!StringUtil.isEmptyOrNull(mchntName)) {
            mchntBaseInfoFilter.setMchntName("%" + mchntName + "%");
        }
        /*商户签约机构过滤条件*/
        if (!StringUtil.isEmptyOrNull(signInstId)) {
            mchntBaseInfoFilter.setSignInstId(signInstId);
        }
        /* 代理机构过滤条件*/
        if (!StringUtil.isEmptyOrNull(agentMchntNo)) {
            mchntBaseInfoFilter.setAgentMchntNo(agentMchntNo);
        }
        /*商户等级过滤条件*/
        if (!StringUtil.isEmptyOrNull(mchntLevel)) {
            mchntBaseInfoFilter.setMchntLevel(mchntLevel);
        }
        /*商圈过滤条件*/
        if (!StringUtil.isEmptyOrNull(mchntArea)) {
            mchntBaseInfoFilter.setMchntArea(mchntArea);
        }
        /*商户状态过滤条件*/
        if (!StringUtil.isEmptyOrNull(mchntStatus)) {
            mchntBaseInfoFilter.setMchntStatus(mchntStatus);
        }
        /*平台商户过滤条件*/
        if (!StringUtil.isEmptyOrNull(platMchntNo)) {
            mchntBaseInfoFilter.setPlatMchntNo(platMchntNo);
        }
        /*当前日期，过滤只导出生效时间<=当前日期<=失效时间的费率*/
        String dateStr = DateUtil.formatDate(new Date(), "yyyyMMdd");
        mchntBaseInfoFilter.setRemark4(dateStr);

        /* 根据用户标识查询用户信息，获取用户组织机构 */
        AuthUserMapper authUserMapper = new AuthUserMapperImpl();
        AuthUser authUser = authUserMapper.selectAuthUserById(userId);
        if (null == authUser) {
            rglog.error("用户信息查询失败");
            throw new BizException(RespCodeEnum.QUERY_AUTH_USER_IS_NULL.getRespCode(), RespCodeEnum.QUERY_AUTH_USER_IS_NULL.getRespDesc());
        }
        String orgCode = authUser.getOrgCode();
        mchntBaseInfoFilter.setRemark5(orgCode);

        /*查询商户基本信息*/
        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        List<ExportMchntBaseInfo> mchntBaseInfoList = new ArrayList<ExportMchntBaseInfo>();
        PageResult pageResult = mchntBaseInfoMapper.selectBompMchntBaseInfoByMchntInfoFilter(instId, mchntBaseInfoFilter, pageInfo);
        if (null == pageResult) {
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        } else {
            mchntBaseInfoList = pageResult.getResult().stream().map(item -> (ExportMchntBaseInfo) item).collect(Collectors.toList());
            pageInfo = pageResult.getPageInfo();
        }

        /*查询行业类别*/
        IndustryInfoMapper industryInfoMapper = new IndustryInfoMapperImpl();
        List<IndustryInfo> industryInfoLst = industryInfoMapper.selectIndustryInfoByKeys(null, null, null, null);
        HashMap<String, String> industryInfoMap = new HashMap<String, String>();
        if (null != industryInfoLst && industryInfoLst.size() > 0) {
            for (IndustryInfo industryInfo : industryInfoLst) {
                industryInfoMap.put(industryInfo.getIndustryType(), industryInfo.getIndustryName());
            }
        }

        /*查询标准产品信息*/
        ProdInfoMapper prodInfoMapper = new ProdInfoMapperImpl();
        List<ProdInfo> prodInfoLst = prodInfoMapper.selectProdInfoByInstId(instId);
        HashMap<String, String> prodInfoMap = new HashMap<String, String>();
        if (null != prodInfoLst && prodInfoLst.size() > 0) {
            for (ProdInfo prodInfo : prodInfoLst) {
                prodInfoMap.put(prodInfo.getProdCode(), prodInfo.getProdDesc());
            }
        }
        /*查询基础产品信息*/
        BaseProdInfoMapper baseProdInfoMapper = new BaseProdInfoMapperImpl();
        List<BaseProdInfo> baseProdInfoLst = baseProdInfoMapper.selectBaseProdInfoByInstId(instId);
        HashMap<String, String> baseProdInfoMap = new HashMap<String, String>();
        if (null != baseProdInfoLst && baseProdInfoLst.size() > 0) {
            for (BaseProdInfo baseProdInfo : baseProdInfoLst) {
                baseProdInfoMap.put(baseProdInfo.getBaseProdCode(), baseProdInfo.getBaseProdName());
            }
        }

        List<ExportMchntInfoBean> mchntInfoList = new ArrayList<>();
        MchntSignInfoMapper mchntSignInfoMapper = new MchntSignInfoMapperImpl();
        MchntAcctInfoMapper mchntAcctInfoMapper = new MchntAcctInfoMapperImpl();
        for (ExportMchntBaseInfo mchntBaseInfo : mchntBaseInfoList) {
            /*将费率类型为07-提现手续费但是关闭了提现标志的数据过滤掉*/
            if (MchntFeeTypeEnum.DO.getType().equals(mchntBaseInfo.getFeeType()) && BompAppParamConstant.FLAG_N.equals(mchntBaseInfo.getCashWithdrawalFlag())) {
                continue;
            }
            ExportMchntInfoBean exportmchntInfoBean = new ExportMchntInfoBean();
            /*银行代码赋值*/
            exportmchntInfoBean.setInstId(instId);
            /*商户号赋值*/
            exportmchntInfoBean.setMchntNo(mchntBaseInfo.getMchntNo());
            /*商户名赋值*/
            exportmchntInfoBean.setMchntName(mchntBaseInfo.getMchntName());
            /*商户简称赋值*/
            exportmchntInfoBean.setMchntSimpleName(mchntBaseInfo.getMchntSimpleName());
            /*收单机构代码赋值*/
            exportmchntInfoBean.setSignInstId(mchntBaseInfo.getSignInstId());
            /*收单机构名称赋值*/
            exportmchntInfoBean.setSignInstName(mchntBaseInfo.getSignInstName());
            /*拓展经理工号*/
            exportmchntInfoBean.setManagerId(mchntBaseInfo.getManagerId());
            /*拓展经理姓名*/
            exportmchntInfoBean.setManagerName(mchntBaseInfo.getManagerName());
            /*行业类别*/
            exportmchntInfoBean.setIndustryType(mchntBaseInfo.getIndustryType());
            /*行业类别名称*/
            exportmchntInfoBean.setIndustryName(industryInfoMap.get(mchntBaseInfo.getIndustryType()));
            /*受理地区赋值*/
            exportmchntInfoBean.setAcqAreaCode(mchntBaseInfo.getMchntArea());
            /*商户状态赋值*/
            exportmchntInfoBean.setMchntStatus(mchntBaseInfo.getMchntStatus());
            /*审核状态*/
            exportmchntInfoBean.setAuditStatus(mchntBaseInfo.getAuditStatus());
            /*商户联系人赋值*/
            exportmchntInfoBean.setContactName(mchntBaseInfo.getMchntContactName());
            /*联系人证件类型*/
            exportmchntInfoBean.setContactCertType(mchntBaseInfo.getMchntContactCertType());
            /*联系人证件号码*/
            exportmchntInfoBean.setContactCertNo(mchntBaseInfo.getMchntContactCertNo());
            /*联系人证件生效日期*/
            exportmchntInfoBean.setContactCertEffectiveDate(mchntBaseInfo.getCertEffectiveDate());
            /*联系人证件失效日期*/
            exportmchntInfoBean.setContactCertExpiryDate(mchntBaseInfo.getCertExpiryDate());
            /*联系电话*/
            exportmchntInfoBean.setContactPhone(mchntBaseInfo.getMchntContactPhone());
            /*联系人手机号*/
            exportmchntInfoBean.setContactMobile(mchntBaseInfo.getMchntContactMobile());
            /*联系人邮箱*/
            exportmchntInfoBean.setContactEmail(mchntBaseInfo.getMchntContactEmail());
            /*营业地址*/
            exportmchntInfoBean.setMchntBizAddr(mchntBaseInfo.getMchntBizAddr());
            /*创建时间*/
            exportmchntInfoBean.setCreatTime(mchntBaseInfo.getCreateTime());
            /*审批通过时间*/
            exportmchntInfoBean.setAuditTime(mchntBaseInfo.getLastAuditTime());

            /*根据商户号查询商户签约信息*/
            MchntSignInfo mchntSignInfo = mchntSignInfoMapper.selectMchntSignInfoByPrimaryKey(instId, mchntBaseInfo.getMchntNo());
            if (null != mchntSignInfo) {
                /*营业证明文件类型赋值*/
                exportmchntInfoBean.setLicenceType(mchntSignInfo.getLicenceType());
                /*营业执照号码赋值*/
                exportmchntInfoBean.setLicenceCode(mchntSignInfo.getLicenceCode());
                /*法人代表姓名赋值*/
                exportmchntInfoBean.setArtifName(mchntSignInfo.getArtifName());
                /*法定代表人证件类型赋值*/
                exportmchntInfoBean.setArtifCertType(mchntSignInfo.getArtifCertType());
                /*法人代表证件号码赋值*/
                exportmchntInfoBean.setArtifCertNo(mchntSignInfo.getArtifCertNo());
                /*法人证件生效日期*/
                exportmchntInfoBean.setCertEffectiveDate(mchntSignInfo.getCertEffectiveDate());
                /*法人证件失效日期*/
                exportmchntInfoBean.setCertExpiryDate(mchntSignInfo.getCertExpiryDate());
                /*法人手机号*/
                exportmchntInfoBean.setArtifMobile(mchntSignInfo.getArtifMobile());
                /*商户注册资金*/
                exportmchntInfoBean.setRegisterCapital(mchntSignInfo.getRegisterCapital());
                /*商户注册地址*/
                exportmchntInfoBean.setRegisterAddr(mchntSignInfo.getRegisterAddr());
            } else {
                rglog.error("查询签约信息失败!" + mchntNo);
            }
            /*根据商户号查询商户结算账户信息*/
            List<MchntAcctInfo> mchntAcctInfoLst = mchntAcctInfoMapper.selectMchntAcctInfoByMchntNo(instId, mchntBaseInfo.getMchntNo());
            if (null != mchntAcctInfoLst && mchntAcctInfoLst.size() > 0) {
                MchntAcctInfo mchntAcctInfo = mchntAcctInfoLst.get(0);
                /*结算账户类型*/
                exportmchntInfoBean.setSettleAcctType(mchntAcctInfo.getSettleAcctType());
                /*结算账号*/
                exportmchntInfoBean.setSettleAcctNo(mchntAcctInfo.getSettleAcctNo());
                /*结算账户户名*/
                exportmchntInfoBean.setSettleAcctName(mchntAcctInfo.getSettleAcctName());
            } else {
                rglog.error("查询结算账户信息失败!" + mchntNo);
            }

            /*费率信息*/
            /*标准产品编码*/
            exportmchntInfoBean.setProdCode(mchntBaseInfo.getProdCode());
            /*标准产品描述*/
            exportmchntInfoBean.setProdDesc(prodInfoMap.get(mchntBaseInfo.getProdCode()));
            /*基础产品代码*/
            exportmchntInfoBean.setBaseProdCode(mchntBaseInfo.getBaseProdCode());
            /*基础产品名称*/
            exportmchntInfoBean.setBaseProdName(baseProdInfoMap.get(mchntBaseInfo.getBaseProdCode()));
            /*费率类型*/
            exportmchntInfoBean.setFeeType(mchntBaseInfo.getFeeType());
            /*费率输入方式*/
            exportmchntInfoBean.setFeeInputMode(mchntBaseInfo.getFeeInputMode());
            /*现有费率代码*/
            exportmchntInfoBean.setFeeCode(mchntBaseInfo.getFeeCode());
            /*费率比率*/
            exportmchntInfoBean.setFeeRate(mchntBaseInfo.getFeeRate());
            /*封顶金额*/
            exportmchntInfoBean.setFeeCappingAmt(mchntBaseInfo.getFeeCappingAmt());
            /*固定金额*/
            exportmchntInfoBean.setFeeFixedAmt(mchntBaseInfo.getFeeFixedAmt());

            mchntInfoList.add(exportmchntInfoBean);
        }
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse<MchntBatchExportResponse> bizResponse = new BizResponse<>();
        /*数据页数赋值*/
        mchntBatchExportResponse.setTotalPage(pageInfo.getTotalPage());
        mchntBatchExportResponse.setPageIndex(pageInfo.getPageIndex());
        mchntBatchExportResponse.setPageSize(pageInfo.getPageSize());
        /*数据条数赋值*/
        mchntBatchExportResponse.setTotalRows(pageInfo.getTotalRows());
        mchntBatchExportResponse.setMchntInfoList(mchntInfoList);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntBatchExportResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntBatchExportResponse mchntBatchExportResponse = (MchntBatchExportResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntBatchExportResponse);
    }
}
